<?php 
//获取最新的token
function check_token(){
	global $table_Prefix;
	$public_id = 'gh_582975265100'; //公众号id
	
	$sqlstr="select * from ".$table_Prefix.'wechat_official'." where  public_id = '$public_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$system_info = mysqli_fetch_assoc($result);
		
	$APPID = $system_info['appid'];
	
	$SERECT = $system_info['appsecret'];
	
	
	//配置参数的数组
	//这些是圆点设计公众号的参数
	$CONF =  array(
	'__APPID__' => $APPID,
	'__SERECT__' => $SERECT
	);
	
	$access_token = '';
	
	if(strtotime($system_info['token_expire'])< time()){ //检查公众号的token是否过期
		
		$token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$CONF['__APPID__'].'&secret='.$CONF['__SERECT__'];
		
		$get_json=file_get_contents($token_url);
		
		$arr = json_decode($get_json,true);
		
		if($public_id){
			$sql = 'update '.$table_Prefix."wechat_official set token = '$arr[access_token]' , token_expire = '".date('YmdHis',time()+7000)."' where public_id = '$public_id' ";
			
			do_query($sql);
		}
		
		$access_token = $arr['access_token'];
		
	}
	else{
		$access_token = $system_info['token'];
	}
	return $access_token;
	
}


function send_activity($themes_id,$av_id){ //活动申请通知后台人员
	
	global $table_Prefix;
	//活动详情
	$sqlstr="select * from ".$table_Prefix.'activity'." where  id = '$av_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$activity_info = mysqli_fetch_assoc($result);
	
	
	//模板信息
	$sqlstr="select * from ".$table_Prefix.'wechat_themes'." where  t_id = '$themes_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$themes_info = mysqli_fetch_assoc($result);
	
	$themes_content = unserialize($themes_info['themes_info']);
		
	$data = array();
	
	//实例数据	
	assign('activity_name',$activity_info['theme']);
	assign('user_name',$activity_info['contact']);
	assign('user_mobile',$activity_info['phone']);	
	//实例数据end	
		
	foreach($themes_content['keywords_name'] as $key=>$val){
		$cont = $themes_content['keywords_desc'][$key];
		$data[$val] = array(
						"value"=>fetch_str($cont), //替换内容
						"color"=>$themes_content['keywords_color'][$key]
		);
				
	}		
	ksort($data); //字段排序
			
	//查找后台发送用户
	$sql ="select * from ".$table_Prefix.'weixin_user'." where fake_id != '' and is_send = 1 ";
	
	$result = do_query($sql);
	 
	while($rows = mysqli_fetch_assoc($result))
	{
		$post_data = array(
					"touser"=>$rows['fake_id'], //推送给谁,openid
					"template_id"=>$themes_info['themes_sn'], //微信后台的模板信息id
					"url"=>$themes_info['themes_url'], 
					"data"=>$data
		);
		
		$content = serialize($post_data);
	
		//插库保存	
		insert_log($rows['fake_id'] , $content , $themes_id);
	}
	
}



function send_register($themes_id,$register_id,$activity_id){  //活动报名申请通知后台人员
	
	global $table_Prefix;
	
	//活动详情
	$sqlstr="select * from ".$table_Prefix.'activity'." where  id = '$activity_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$activity_info = mysqli_fetch_assoc($result);
	
	
	//报名内容
	$sqlstr="select * from ".$table_Prefix.'register'." where  nid = '$register_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$register_info = mysqli_fetch_assoc($result);
	
	
	//模板信息
	$sqlstr="select * from ".$table_Prefix.'wechat_themes'." where  t_id = '$themes_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$themes_info = mysqli_fetch_assoc($result);
	
	$themes_content = unserialize($themes_info['themes_info']);
		
	$data = array();
	
	//实例数据	
	assign('activity_name',$activity_info['theme']);
	assign('name',$register_info['name']);
	assign('phone',$register_info['phone']);
	
	//实例数据end	
		
	foreach($themes_content['keywords_name'] as $key=>$val){
		$cont = $themes_content['keywords_desc'][$key];
		$data[$val] = array(
						"value"=>fetch_str($cont), //替换内容
						"color"=>$themes_content['keywords_color'][$key]
		);
				
	}		
	ksort($data); //字段排序
			
	//查找后台发送用户
	$sql ="select * from ".$table_Prefix.'weixin_user'." where fake_id != '' and is_send = 1 ";
	
	$result = do_query($sql);
	 
	while($rows = mysqli_fetch_assoc($result))
	{
		$post_data = array(
					"touser"=>$rows['fake_id'], //推送给谁,openid
					"template_id"=>$themes_info['themes_sn'], //微信后台的模板信息id
					"url"=>$themes_info['themes_url'], 
					"data"=>$data
		);
		
		$content = serialize($post_data);
	
		//插库保存	
		insert_log($rows['fake_id'] , $content , $themes_id);
	}
}



function send_activity_user($themes_id,$av_id){ //活动申请通知申请人
	
	global $table_Prefix;
	//活动详情
	$sqlstr="select * from ".$table_Prefix.'activity'." where  id = '$av_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$activity_info = mysqli_fetch_assoc($result);
	
	
	//模板信息
	$sqlstr="select * from ".$table_Prefix.'wechat_themes'." where  t_id = '$themes_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$themes_info = mysqli_fetch_assoc($result);
	
	$themes_content = unserialize($themes_info['themes_info']);
		
	$data = array();
	
	//实例数据	
	$status = '';
	if($activity_info['status'] ==  0 ){
		$status = '待审核';
	}
	elseif($activity_info['status'] ==  1 ){
		$status = '通过';
	}
	elseif($activity_info['status'] ==  2 ){
		$status = '未通过';
	}
	
	assign('activity',$activity_info['theme']);
	assign('status',$status);
	assign('date',date('Y-m-d H:i:s'));	
	assign('reason',$activity_info['reason']);
	//实例数据end	
		
	foreach($themes_content['keywords_name'] as $key=>$val){
		$cont = $themes_content['keywords_desc'][$key];
		$data[$val] = array(
						"value"=>fetch_str($cont), //替换内容
						"color"=>$themes_content['keywords_color'][$key]
		);
				
	}		
	ksort($data); //字段排序
			
	//发送用户
	
	$post_data = array(
				"touser"=>$activity_info['open_id'], //推送给谁,openid
				"template_id"=>$themes_info['themes_sn'], //微信后台的模板信息id
				"url"=>$themes_info['themes_url'], 
				"data"=>$data
	);
		
	$content = serialize($post_data);
	
	//插库保存	
	insert_log($rows['fake_id'] , $content , $themes_id);
	
}


function send_register_user($themes_id,$user_id){ //活动报名通知申请人
	
	global $table_Prefix;
	
	//报名详情
	$sqlstr="select * from ".$table_Prefix.'register'." where  nid = '$user_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$register_info = mysqli_fetch_assoc($result);
	
	//活动详情
	$sqlstr="select * from ".$table_Prefix.'activity'." where  id = '$register_info[activity_id]'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$activity_info = mysqli_fetch_assoc($result);

	
	//模板信息
	$sqlstr="select * from ".$table_Prefix.'wechat_themes'." where  t_id = '$themes_id'  limit 1 ";

	$result = do_query($sqlstr);
	 
	$themes_info = mysqli_fetch_assoc($result);
	
	$themes_content = unserialize($themes_info['themes_info']);
		
	$data = array();
	
	//实例数据	
	
	$status = '';
	if($register_info['status'] ==  0 ){
		$status = '待审核';
	}
	elseif($register_info['status'] ==  1 ){
		$status = '通过';
	}
	elseif($register_info['status'] ==  2 ){
		$status = '未通过';
	}
	
	assign('status',$status);
	assign('date',date('Y-m-d H:i:s'));		
	assign('activity',$activity_info['theme']);
	assign('reason',$register_info['reason']);
	//实例数据end	
		
	foreach($themes_content['keywords_name'] as $key=>$val){
		$cont = $themes_content['keywords_desc'][$key];
		$data[$val] = array(
						"value"=>fetch_str($cont), //替换内容
						"color"=>$themes_content['keywords_color'][$key]
		);
				
	}		
	ksort($data); //字段排序
			
	//发送用户
	
	$post_data = array(
				"touser"=>$register_info['open_id'], //推送给谁,openid
				"template_id"=>$themes_info['themes_sn'], //微信后台的模板信息id
				"url"=>$themes_info['themes_url'], 
				"data"=>$data
	);
		
	$content = serialize($post_data);
	
	//插库保存	
	insert_log($rows['fake_id'] , $content , $themes_id);
	
}

//发送信息
function curlPost($url = '', $data = '')
{
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

	$result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

//发送
function send_now($access_token,$data,$send_id){
	
	global $table_Prefix;
	
	$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

	$result = curlPost($url, $data );
	
	$json = json_decode($result,true);

	if (!$json || !empty($json['errcode'])) {
		
	}
	
	$info['send_time']=time();
	
	$info['reback_content']=serialize($json);
	
	$info['reback_error']=$json['errcode'];
	
	$sql = 'update '.$table_Prefix."send_list set send_time = '$info[send_time]' , reback_content = '$info[reback_content]'  , reback_error = '$info[reback_error]'   where send_id = '$send_id' ";
			
	do_query($sql);
	
}

function insert_log($open_id , $content , $themes_id){
	
	global $table_Prefix;
	
	$sql = "INSERT INTO `".$table_Prefix."send_list`( `open_id`, `content`, `themes_id`) VALUES ('".$open_id."','".$content."','".$themes_id."')";
	
	
	do_query($sql);
	
	$send_id = mysqli_insert_id();
	
	$access_token = check_token();//获取最新的token
	
	$json_send_data = json_encode( unserialize($content));
	
	if($access_token != '' && $json_send_data != '' && $send_id > 0){
		send_now($access_token,$json_send_data,$send_id); //即时发送
	}
	
}




function fetch_str($source)
{
	return preg_replace("/{([^\}\{\n]*)}/e", "\select('\\1');", $source);
}

function select($tag)
{
	
	global $tmp_var;
    $tag = stripslashes(trim($tag));
		
    if ($tag{0} == '$') // 变量
    {
		  
		return $tmp_var[substr($tag, 1)];
          
    }
       
}

function assign($tpl_var, $value = '')
{
  global $tmp_var;
  if ($tpl_var != '')
  {
      $tmp_var[$tpl_var] = $value;
  }
       
}


?>